package jdk21.switchs;

import jdk21.record.Point;

/**
 * @description: switch 语句
 * @author: grf
 * @date: 2024/8/22
 */
public class PatternMatching {
    public static void main(String[] args) {
        System.out.println(describe(1L));
        System.out.println(describePoint(new Point(1, 2)));
        System.out.println(classify("Hello"));
    }
    /** 
     * @description: 基本示例
     * @author: grf
     * @date: 2024/8/22
     */
    public static String describe(Object obj) {
        return switch (obj) {
            case Integer i -> "Integer " + i;
            case Long l    -> "Long " + l;
            case Double d  -> "Double " + d;
            case String s  -> "String " + s;
            default        -> "Unknown type";
        };
    }
    /** 
     * @description: 模式结构
     * @author: grf
     * @date: 2024/8/22
     */
    public static String describePoint(Point p) {
        return switch (p) {
            case Point(int x, int y) when x == y -> "Point on the diagonal";
            case Point(int x, int y) -> "Point at (" + x + ", " + y + ")";
            default -> "Unknown point";
        };
    }
    /**
     * @description: when子句
     * @author: grf
     * @date: 2024/8/22
     */
    public static String classify(Object obj) {
        return switch (obj) {
            case String s when s.length() > 10 -> "Long string";
            case String s -> "Short string";
            case Integer i when i > 0 -> "Positive integer";
            case Integer i -> "Non-positive integer";
            default -> "Unknown";
        };
    }
}
